package com.ja.openchat.ui.me;

import android.text.TextUtils;

import com.dueeeke.videoplayer.util.PlayerUtils;
import com.zy.openchat.App;
import com.zy.openchat.Constants;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.util.SPUtils;

public class SettingManager {
    private volatile static SettingManager manager;

    private SettingManager() {
        init();
    }

    public static SettingManager getInstance() {
        if (manager == null) {
            synchronized (SettingManager.class) {
                if (manager == null) {
                    manager = new SettingManager();
                }
            }
        }
        return manager;
    }

    public boolean canDownload(TelegramManager.FileType type){
        if(type==null
                ||type == TelegramManager.FileType.FileType_Group_Photo
                ||type == TelegramManager.FileType.FileType_Photo
                ||type == TelegramManager.FileType.FileType_Message_Audio
                ||type == TelegramManager.FileType.FileType_Message_Voice
                ||type == TelegramManager.FileType.FileType_Message_Animation){
            return true;
        }
        int networkType = PlayerUtils.getNetworkType(App.getInstanse());
        if(networkType == PlayerUtils.NETWORK_CLOSED
                ||networkType == PlayerUtils.NETWORK_ETHERNET
                ||networkType == PlayerUtils.NETWORK_UNKNOWN){
            return true;
        }
        if(networkType == PlayerUtils.NETWORK_MOBILE){
            return (((type== TelegramManager.FileType.FileType_Message_Photo||type== TelegramManager.FileType.FileType_Message_Preview_Photo)&&mobileImageEnable)
                    ||(type== TelegramManager.FileType.FileType_Message_Video&&mobileVideoEnable)
                    ||(type== TelegramManager.FileType.FileType_Message_Document&&mobileFileEnable));
        }else{
            return (((type== TelegramManager.FileType.FileType_Message_Photo||type== TelegramManager.FileType.FileType_Message_Preview_Photo)&&wifiImageEnable)
                    ||(type== TelegramManager.FileType.FileType_Message_Video&&wifiVideoEnable)
                    ||(type== TelegramManager.FileType.FileType_Message_Document&&wifiFileEnable));
        }
    }

    private boolean autoPlayGif = true;

    private boolean mobileImageEnable = false;
    private boolean mobileVideoEnable = false;
    private boolean mobileFileEnable = false;

    private boolean wifiImageEnable = false;
    private boolean wifiVideoEnable = false;
    private boolean wifiFileEnable = false;

    private void init(){
        initNetworkDownload();
        initPlayGif();
    }

    public boolean isAutoPlayGif(){
        return autoPlayGif;
    }

    public void initPlayGif(){
        autoPlayGif = SPUtils.getBoolean(App.getInstanse(),Constants.SPU_FOR_AUTO_PLAY_GIF,true);
    }

    public void initNetworkDownload(){
        String mobileValue = SPUtils.getString(App.getInstanse(), Constants.SPU_FOR_USE_MOBILE_NETWORK,UseMobileNetworkActivity.ALL_OPEN);
        mobileImageEnable = false;
        mobileVideoEnable = false;
        mobileFileEnable = false;
        if(!TextUtils.isEmpty(mobileValue)){
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_IMAGE))){
                mobileImageEnable = true;
            }
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_VIDEO))){
                mobileVideoEnable = true;
            }
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_FILE))){
                mobileFileEnable = true;
            }
        }
        String wifiValue = SPUtils.getString(App.getInstanse(), Constants.SPU_FOR_USE_WIFI_NETWORK,UseMobileNetworkActivity.ALL_OPEN);
        wifiImageEnable = false;
        wifiVideoEnable = false;
        wifiFileEnable = false;
        if(!TextUtils.isEmpty(wifiValue)){
            if(wifiValue.contains(String.valueOf(Constants.NETWORK_FOR_IMAGE))){
                wifiImageEnable = true;
            }
            if(wifiValue.contains(String.valueOf(Constants.NETWORK_FOR_VIDEO))){
                wifiVideoEnable = true;
            }
            if(wifiValue.contains(String.valueOf(Constants.NETWORK_FOR_FILE))){
                wifiFileEnable = true;
            }
        }

    }
    
}
